package com.scpii.universal.pns.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;

import com.scpii.universal.hibernate.entity.BaseEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MessageDto extends BaseEntity {
	
	private Integer messageId;
	private Long id;
	private String appId;
	private Integer buildPlatform;
	private String title;
	private String icon;
	private String summary;
	private String body;
	private Date startDate;
	private Date expireDate;
	private Date createDate;
	private Integer messageStatus;
	
	private List<MessgeBodyDto> bodyDto;
	
	public void msgBodyToList() {
		JSONArray ja = JSONArray.fromObject(body);
		MessgeBodyDto messgeBodyDto = null;
		JSONObject jo = null;
		if (bodyDto == null)
			bodyDto = new ArrayList<MessgeBodyDto>();
		int size = ja.size();
		for (int i = 0; i < size; i++) {
			messgeBodyDto = new MessgeBodyDto();
			jo = JSONObject.fromObject(ja.get(i));
			int type = Integer.parseInt(jo.getString("type"));
			messgeBodyDto.setContentType(type);
			String content = jo.getString("content");
			if (type == 1) {
				content = getServerUrl() + content + "?rand="+Math.random()*100;
			}
			if (type == 0 && !StringUtils.isBlank(content)) {
				content = content.replaceAll("\\\\n", "\n").replaceAll("\\\\r","\r").replaceAll("\\\\\"", "\"");
			}
			messgeBodyDto.setContent(content);
			bodyDto.add(messgeBodyDto);
		}
	}
	
	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public Integer getBuildPlatform() {
		return buildPlatform;
	}
	public void setBuildPlatform(Integer buildPlatform) {
		this.buildPlatform = buildPlatform;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getExpireDate() {
		return expireDate;
	}
	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Integer getMessageStatus() {
		return messageStatus;
	}
	public void setMessageStatus(Integer messageStatus) {
		this.messageStatus = messageStatus;
	}
	public List<MessgeBodyDto> getBodyDto() {
		return bodyDto;
	}
	public void setBodyDto(List<MessgeBodyDto> bodyDto) {
		this.bodyDto = bodyDto;
	}

	public Integer getMessageId() {
		return messageId;
	}

	public void setMessageId(Integer messageId) {
		this.messageId = messageId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
